<template>
  <Header :title="title"></Header>
  <div
    class="fui-page order-create-page fui-page-current"
    style="height: 100%; margin-top: 40px"
  >
    <div class="fui-content navbar">
      <!-- 地址 -->
      <div
        class="fui-list-group"
        @click="
          router.push({
            path: '/my_address',
          })
        "
      >
        <div class="fui-list">
          <div class="fui-list-inner">
            <div class="title">
              收货人：
              <span class="realname">{{ defaultAddr.name }}</span>
              <span class="mobile">{{ defaultAddr.phone }}</span>
            </div>
            <div class="title">
              <span class="address">{{ defaultAddr.address }}</span>
            </div>
          </div>
          <div class="address_img">
            <img src="@/assets/images/right.png" />
          </div>
        </div>
      </div>
      <!-- 地址end -->
      <!-- 商品 -->
      <div class="fui-shop">
        <div class="fui-shop-list" v-for="good in goods" :key="good.id">
          <div class="fui-shop-list-media">
            <img class="img" :src="good.img?.split(';')[0]" />
          </div>
          <div class="shops" style="folat: left; max-width: 200px">
            <div class="subtitle">
              {{ good.title }}
            </div>
          </div>
          <div class="fui-list-angle">
            <span style="font-size: 0.65rem; color: #000"
              >￥<span class="marketprice">{{ good.price }}</span></span
            >
            <div style="color: #999">{{ good.order_num }}</div>
          </div>
        </div>
      </div>
      <!-- 商品end -->
      <!-- 买家留言 -->
      <div class="fui-cell-group">
        <div class="fui-cell fui-cell-textarea">
          <div class="fui-cell-label">买家留言</div>
          <div class="fui-cell-info">
            <textarea
              class="textarea"
              rows="2"
              placeholder="50字以内（选填）"
              v-model="marks"
            ></textarea>
          </div>
        </div>
      </div>
      <!-- 买家留言end -->
      <!-- 支付按钮 -->
      <div class="fui-navbar">
        <div class="nav-item total">
          需付：<span class="text-danger size0-75rem">¥ {{ totalMoney }}</span>
          <button
            class="nav-item btn btn-danger buybtn"
            style="width: 200px; margin-left: 10px"
            @click="confirm()"
          >
            确定订单
          </button>
        </div>
      </div>
      <!-- 支付按钮end -->
    </div>
  </div>
</template>
<script>
import { reactive, toRefs, computed } from "vue";
import Header from "../../components/Header.vue";
import { useRoute, useRouter } from "vue-router";
import { shopOne, addOrder, shopMany } from "../../network/index.js";

export default {
  components: {
    Header,
  },
  setup() {
    const route = useRoute();
    const router = useRouter();
    const data = new reactive({
      goods: [],
      address: [],
      defaultAddr: {},
      marks: "",
      totalMoney: 0,
    });

    const confirm = () => {
      // 直接购买
      if (route.query.type == 1) {
        addOrder({
          address: data.defaultAddr.id,
          id: route.query.id,
          marks: data.marks,
          type: 1,
        }).then((e) => {
          router.push({
            path: "/cart_pay",
            query: { id: e.order_id },
          });
        });
      } else if (route.query.type == 2) {
        // 购物车购买
        addOrder({
          address: data.defaultAddr.id,
          id: route.query.id,
          num: route.query.num,
          marks: data.marks,
          type: 2,
        }).then((e) => {
          router.push({
            path: "/cart_pay",
            query: { id: e.order_id },
          });
        });
      }
    };

    // 获取订单信息
    // 直接购买
    if (route.query.type == 1) {
      shopOne({
        id: route.query.id,
      }).then((e) => {
        // 订单信息
        data.goods = [e.good];
        // 地址
        data.address = e.address;
        data.defaultAddr = data.address.find((item) => item.is === 1);
      });
      // 购物车购买
    } else if (route.query.type == 2) {
      shopMany({
        id: route.query.id,
        num: route.query.num,
      }).then((e) => {
        data.totalMoney = e.good.total_money;
        delete e.good.total_money;
        data.goods = Object.values(e.good);
        data.address = e.address;
        data.defaultAddr = data.address.find((item) => item.is === 1);
      });
    }

    return {
      title: "下单",
      confirm,
      ...toRefs(data),
      router,
    };
  },
};
</script>
<style scoped>
.fui-page {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #f3f3f3;
  box-sizing: border-box;
  display: none;
}
.order-create-page {
  margin: 0;
  bottom: 2rem;
  padding-bottom: 2rem;
  display: block;
  overflow: hidden;
}
.fui-content {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.fui-content.navbar {
  bottom: 2.5rem;
  padding-bottom: 2.2rem;
}
/* 地址 */
.fui-list-group {
  background-color: #fff;
  position: relative;
  margin: 0 0 0.5rem;
}
.fui-list {
  position: relative;
  display: flex;
  padding: 0.4rem 0.6rem;
  transition-duration: 300ms;
  align-items: center;
  overflow: hidden;
  padding-top: 0.8rem;
  padding-bottom: 0.83rem;
}
.fui-list-inner {
  position: relative;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;
  -ms-flex: 1;
  flex: 1;
  display: block;
  align-self: flex-start;
}
.fui-list-inner .text {
  position: relative;
  font-size: 0.7rem;
  color: #000;
  text-align: center;
}
.fui-list-inner .title {
  position: relative;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  display: flex;
  color: #000;
  font-size: 0.7rem;
  line-height: 1.2rem;
  height: 1.2rem;
}
.realname {
  display: inline-block;
  max-width: 9rem;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  padding-right: 1.2rem;
}
.mobile {
  display: inline-block;
  overflow: hidden;
}
.address {
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 0.65rem;
  color: #666;
}
.address_img {
  float: right;
  display: block;
}
/* 地址end */
/* 商品 */
.fui-shop {
  background-color: #fff;
  position: relative;
  margin-top: 0.5rem;
}
.fui-shop-list {
  align-items: center;
  overflow: hidden;
  transition-duration: 300ms;
  display: flex;
  padding: 0.4rem 0.6rem;
  position: relative;
  -webkit-box-align: start;
}
.fui-shop-list-media {
  display: flex;
  align-items: center;
  margin-right: 0.6rem;
  color: #aaa;
  position: relative;
  flex-wrap: nowrap;
  box-sizing: border-box;
  -webkit-box-align: center;
  flex-shrink: 0;
  -webkit-box-lines: single;
}
.img {
  width: 4rem;
  height: 4rem;
}
.shops {
  height: 3.5rem;
  align-self: center;
  justify-content: space-between;
  flex-direction: column;
  -webkit-box-pack: justify;
  display: flex;
}
.subtitle {
  color: #000;
  font-size: 0.7rem;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
}
.fui-list-angle {
  text-align: right;
  font-size: 0.7rem;
  color: #666;
  margin-right: 0.1rem;
  right: 20px;
  position: absolute;
  vertical-align: middle;
  height: 3.5rem;
  align-self: center;
  width: 3.5rem;
}
/* 买家留言 */
.fui-cell-group {
  margin-top: 0.5rem;
  background-color: #fff;
  line-height: 1.4;
  font-size: 0.8rem;
  overflow: hidden;
  position: relative;
  display: block;
}
.fui-cell-group .fui-cell {
  position: relative;
  padding: 0.75rem 0.6rem 0.65rem;
  display: flex;
  transition-duration: 300ms;
  line-height: 1.2;
  height: 2.5rem;
  align-items: flex-start;
}
.fui-cell-group .fui-cell .fui-cell-label {
  position: relative;
  display: block;
  width: 3.85rem;
  font-size: 0.7rem;
  color: #666;
  margin: 0.15rem 0 0 0;
}
.fui-cell-group .fui-cell .fui-cell-info {
  -webkit-box-flex: 1;
  flex: 1;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  font-size: 0.7rem;
  color: #000;
}
.textarea {
  width: 100%;
  background: transparent;
  border: none;
  resize: none;
  font-size: 0.7rem;
  font-family: "Helvetica Neue", Helvetica, sans-serif;
}
/* 支付按钮 */
.fui-navbar {
  position: fixed;
  width: 100%;
  bottom: 0;
  height: 2.45rem;
  background: #fff;
  z-index: 2;
}
.fui-navbar .nav-item {
  position: relative;
  display: table-cell;
  height: 2.45rem;
  text-align: center;
  vertical-align: middle;
  width: 1%;
  color: #999;
}
.total {
  padding-left: 0;
  padding-right: 0.5rem;
  font-size: 0.7rem;
  height: 2.45rem;
}
.order-create-page .buybtn {
  border: none;
  font-size: 0.7rem;
  color: #fff;
  border-radius: 0;
  width: 0.2%;
}
.fui-navbar .nav-item.btn {
  color: #fff;
  border-radius: 0;
}
.btn.btn-danger {
  background: #ff5555;
  color: #fff;
  border: 1px solid #ff5555;
}
</style>